!function(e){"object"==typeof exports&&"object"==typeof module?e(require("../../lib/codemirror")):"function"==typeof define&&define.amd?define("addon/search/searchcursor",["../../lib/codemirror"],e):e(CodeMirror)}(function(e){"use strict";function n(e){var n=e.flags;return null!=n?n:(e.ignoreCase?"i":"")+(e.global?"g":"")+(e.multiline?"m":"")}function t(e){return e.global?e:new RegExp(e.source,n(e)+"g")}function o(e){return/\\s|\\n|\n|\\W|\\D|\[\^/.test(e.source)}function r(e,n,o){n=t(n);for(var r=o.line,i=o.ch,s=e.lastLine();r<=s;r++,i=0){n.lastIndex=i;var c=e.getLine(r),l=n.exec(c);if(l)return{from:g(r,l.index),to:g(r,l.index+l[0].length),match:l}}}function i(e,n,i){if(!o(n))return r(e,n,i);n=t(n);for(var s,c=1,l=i.line,a=e.lastLine();l<=a;){for(var u=0;u<c;u++){var f=e.getLine(l++);s=null==s?f:s+"\n"+f}c*=2,n.lastIndex=i.ch;var h=n.exec(s);if(h){var d=s.slice(0,h.index).split("\n"),p=h[0].split("\n"),m=i.line+d.length-1,v=d[d.length-1].length;return{from:g(m,v),to:g(m+p.length-1,1==p.length?v+p[0].length:p[p.length-1].length),match:h}}}}function s(e,n){for(var t,o=0;;){n.lastIndex=o;var r=n.exec(e);if(!r)return t;if(t=r,(o=t.index+(t[0].length||1))==e.length)return t}}function c(e,n,o){n=t(n);for(var r=o.line,i=o.ch,c=e.firstLine();r>=c;r--,i=-1){var l=e.getLine(r);i>-1&&(l=l.slice(0,i));var a=s(l,n);if(a)return{from:g(r,a.index),to:g(r,a.index+a[0].length),match:a}}}function l(e,n,o){n=t(n);for(var r,i=1,c=o.line,l=e.firstLine();c>=l;){for(var a=0;a<i;a++){var u=e.getLine(c--);r=null==r?u.slice(0,o.ch):u+"\n"+r}i*=2;var f=s(r,n);if(f){var h=r.slice(0,f.index).split("\n"),d=f[0].split("\n"),p=c+h.length,m=h[h.length-1].length;return{from:g(p,m),to:g(p+d.length-1,1==d.length?m+d[0].length:d[d.length-1].length),match:f}}}}function a(e,n,t,o){if(e.length==n.length)return t;for(var r=0,i=t+Math.max(0,e.length-n.length);;){if(r==i)return r;var s=r+i>>1,c=o(e.slice(0,s)).length;if(c==t)return s;c>t?i=s:r=s+1}}function u(e,n,t,o){if(!n.length)return null;var r=o?d:p,i=r(n).split(/\r|\n\r?/);e:for(var s=t.line,c=t.ch,l=e.lastLine()+1-i.length;s<=l;s++,c=0){var u=e.getLine(s).slice(c),f=r(u);if(1==i.length){var h=f.indexOf(i[0]);if(-1==h)continue e;var t=a(u,f,h,r)+c;return{from:g(s,a(u,f,h,r)+c),to:g(s,a(u,f,h+i[0].length,r)+c)}}var m=f.length-i[0].length;if(f.slice(m)==i[0]){for(var v=1;v<i.length-1;v++)if(r(e.getLine(s+v))!=i[v])continue e;var y=e.getLine(s+i.length-1),x=r(y),C=i[i.length-1];if(x.slice(0,C.length)==C)return{from:g(s,a(u,f,m,r)+c),to:g(s+i.length-1,a(y,x,C.length,r))}}}}function f(e,n,t,o){if(!n.length)return null;var r=o?d:p,i=r(n).split(/\r|\n\r?/);e:for(var s=t.line,c=t.ch,l=e.firstLine()-1+i.length;s>=l;s--,c=-1){var u=e.getLine(s);c>-1&&(u=u.slice(0,c));var f=r(u);if(1==i.length){var h=f.lastIndexOf(i[0]);if(-1==h)continue e;return{from:g(s,a(u,f,h,r)),to:g(s,a(u,f,h+i[0].length,r))}}var m=i[i.length-1];if(f.slice(0,m.length)==m){for(var v=1,t=s-i.length+1;v<i.length-1;v++)if(r(e.getLine(t+v))!=i[v])continue e;var y=e.getLine(s+1-i.length),x=r(y);if(x.slice(x.length-i[0].length)==i[0])return{from:g(s+1-i.length,a(y,x,y.length-i[0].length,r)),to:g(s,a(u,f,m.length,r))}}}}function h(e,n,o,s){this.atOccurrence=!1,this.doc=e,o=o?e.clipPos(o):g(0,0),this.pos={from:o,to:o};var a;"object"==typeof s?a=s.caseFold:(a=s,s=null),"string"==typeof n?(null==a&&(a=!1),this.matches=function(t,o){return(t?f:u)(e,n,o,a)}):(n=t(n),s&&!1===s.multiline?this.matches=function(t,o){return(t?c:r)(e,n,o)}:this.matches=function(t,o){return(t?l:i)(e,n,o)})}var d,p,g=e.Pos;String.prototype.normalize?(d=function(e){return e.normalize("NFD").toLowerCase()},p=function(e){return e.normalize("NFD")}):(d=function(e){return e.toLowerCase()},p=function(e){return e}),h.prototype={findNext:function(){return this.find(!1)},findPrevious:function(){return this.find(!0)},find:function(n){for(var t=this.matches(n,this.doc.clipPos(n?this.pos.from:this.pos.to));t&&0==e.cmpPos(t.from,t.to);)n?t.from.ch?t.from=g(t.from.line,t.from.ch-1):t=t.from.line==this.doc.firstLine()?null:this.matches(n,this.doc.clipPos(g(t.from.line-1))):t.to.ch<this.doc.getLine(t.to.line).length?t.to=g(t.to.line,t.to.ch+1):t=t.to.line==this.doc.lastLine()?null:this.matches(n,g(t.to.line+1,0));if(t)return this.pos=t,this.atOccurrence=!0,this.pos.match||!0;var o=g(n?this.doc.firstLine():this.doc.lastLine()+1,0);return this.pos={from:o,to:o},this.atOccurrence=!1},from:function(){if(this.atOccurrence)return this.pos.from},to:function(){if(this.atOccurrence)return this.pos.to},replace:function(n,t){if(this.atOccurrence){var o=e.splitLines(n);this.doc.replaceRange(o,this.pos.from,this.pos.to,t),this.pos.to=g(this.pos.from.line+o.length-1,o[o.length-1].length+(1==o.length?this.pos.from.ch:0))}}},e.defineExtension("getSearchCursor",function(e,n,t){return new h(this.doc,e,n,t)}),e.defineDocExtension("getSearchCursor",function(e,n,t){return new h(this,e,n,t)}),e.defineExtension("selectMatches",function(n,t){for(var o=[],r=this.getSearchCursor(n,this.getCursor("from"),t);r.findNext()&&!(e.cmpPos(r.to(),this.getCursor("to"))>0);)o.push({anchor:r.from(),head:r.to()});o.length&&this.setSelections(o,0)})}),function(e){"object"==typeof exports&&"object"==typeof module?e(require("../../lib/codemirror")):"function"==typeof define&&define.amd?define("addon/dialog/dialog",["../../lib/codemirror"],e):e(CodeMirror)}(function(e){function n(e,n,t){var o,r=e.getWrapperElement();return o=r.appendChild(document.createElement("div")),o.className=t?"CodeMirror-dialog CodeMirror-dialog-bottom":"CodeMirror-dialog CodeMirror-dialog-top","string"==typeof n?o.innerHTML=n:o.appendChild(n),o}function t(e,n){e.state.currentNotificationClose&&e.state.currentNotificationClose(),e.state.currentNotificationClose=n}e.defineExtension("openDialog",function(o,r,i){function s(e){if("string"==typeof e)f.value=e;else{if(a)return;a=!0,l.parentNode.removeChild(l),u.focus(),i.onClose&&i.onClose(l)}}i||(i={}),t(this,null);var c,l=n(this,o,i.bottom),a=!1,u=this,f=l.getElementsByTagName("input")[0];return f?(f.focus(),i.value&&(f.value=i.value,!1!==i.selectValueOnOpen&&f.select()),i.onInput&&e.on(f,"input",function(e){i.onInput(e,f.value,s)}),i.onKeyUp&&e.on(f,"keyup",function(e){i.onKeyUp(e,f.value,s)}),e.on(f,"keydown",function(n){i&&i.onKeyDown&&i.onKeyDown(n,f.value,s)||((27==n.keyCode||!1!==i.closeOnEnter&&13==n.keyCode)&&(f.blur(),e.e_stop(n),s()),13==n.keyCode&&r(f.value,n))}),!1!==i.closeOnBlur&&e.on(f,"blur",s)):(c=l.getElementsByTagName("button")[0])&&(e.on(c,"click",function(){s(),u.focus()}),!1!==i.closeOnBlur&&e.on(c,"blur",s),c.focus()),s}),e.defineExtension("openConfirm",function(o,r,i){function s(){a||(a=!0,c.parentNode.removeChild(c),u.focus())}t(this,null);var c=n(this,o,i&&i.bottom),l=c.getElementsByTagName("button"),a=!1,u=this,f=1;l[0].focus();for(var h=0;h<l.length;++h){var d=l[h];!function(n){e.on(d,"click",function(t){e.e_preventDefault(t),s(),n&&n(u)})}(r[h]),e.on(d,"blur",function(){--f,setTimeout(function(){f<=0&&s()},200)}),e.on(d,"focus",function(){++f})}}),e.defineExtension("openNotification",function(o,r){function i(){l||(l=!0,clearTimeout(s),c.parentNode.removeChild(c))}t(this,i);var s,c=n(this,o,r&&r.bottom),l=!1,a=r&&void 0!==r.duration?r.duration:5e3;return e.on(c,"click",function(n){e.e_preventDefault(n),i()}),a&&(s=setTimeout(i,a)),i})}),function(e){"object"==typeof exports&&"object"==typeof module?e(require("../../lib/codemirror"),require("./searchcursor"),require("../dialog/dialog")):"function"==typeof define&&define.amd?define("addon/search/search.js",["../../lib/codemirror","./searchcursor","../dialog/dialog"],e):e(CodeMirror)}(function(e){"use strict";function n(e,n){return"string"==typeof e?e=new RegExp(e.replace(/[\-\[\]\/\{\}\(\)\*\+\?\.\\\^\$\|]/g,"\\$&"),n?"gi":"g"):e.global||(e=new RegExp(e.source,e.ignoreCase?"gi":"g")),{token:function(n){e.lastIndex=n.pos;var t=e.exec(n.string);if(t&&t.index==n.pos)return n.pos+=t[0].length||1,"searching";t?n.pos=t.index:n.skipToEnd()}}}function t(){this.posFrom=this.posTo=this.lastQuery=this.query=null,this.overlay=null}function o(e){return e.state.search||(e.state.search=new t)}function r(e){return"string"==typeof e&&e==e.toLowerCase()}function i(e,n,t){return e.getSearchCursor(n,t,{caseFold:r(n),multiline:!0})}function s(e,n,t,o,r){e.openDialog(n,o,{value:t,selectValueOnOpen:!0,closeOnEnter:!1,onClose:function(){p(e)},onKeyDown:r})}function c(e,n,t,o,r){e.openDialog?e.openDialog(n,r,{value:o,selectValueOnOpen:!0}):r(prompt(t,o))}function l(e,n,t,o){e.openConfirm?e.openConfirm(n,o):confirm(t)&&o[0]()}function a(e){return e.replace(/\\(.)/g,function(e,n){return"n"==n?"\n":"r"==n?"\r":n})}function u(e){var n=e.match(/^\/(.*)\/([a-z]*)$/);if(n)try{e=new RegExp(n[1],-1==n[2].indexOf("i")?"":"i")}catch(e){}else e=a(e);return("string"==typeof e?""==e:e.test(""))&&(e=/x^/),e}function f(e,t,o){t.queryText=o,t.query=u(o),e.removeOverlay(t.overlay,r(t.query)),t.overlay=n(t.query,r(t.query)),e.addOverlay(t.overlay),e.showMatchesOnScrollbar&&(t.annotate&&(t.annotate.clear(),t.annotate=null),t.annotate=e.showMatchesOnScrollbar(t.query,r(t.query)))}function h(n,t,r,i){var l=o(n);if(l.query)return d(n,t);var a=n.getSelection()||l.lastQuery;if(a instanceof RegExp&&"x^"==a.source&&(a=null),r&&n.openDialog){var u=null,h=function(t,o){e.e_stop(o),t&&(t!=l.queryText&&(f(n,l,t),l.posFrom=l.posTo=n.getCursor()),u&&(u.style.opacity=1),d(n,o.shiftKey,function(e,t){var o;t.line<3&&document.querySelector&&(o=n.display.wrapper.querySelector(".CodeMirror-dialog"))&&o.getBoundingClientRect().bottom-4>n.cursorCoords(t,"window").top&&((u=o).style.opacity=.4)}))};s(n,v,a,h,function(t,r){var i=e.keyName(t),s=n.getOption("extraKeys"),c=s&&s[i]||e.keyMap[n.getOption("keyMap")][i];"findNext"==c||"findPrev"==c||"findPersistentNext"==c||"findPersistentPrev"==c?(e.e_stop(t),f(n,o(n),r),n.execCommand(c)):"find"!=c&&"findPersistent"!=c||(e.e_stop(t),h(r,t))}),i&&a&&(f(n,l,a),d(n,t))}else c(n,v,"Search for:",a,function(e){e&&!l.query&&n.operation(function(){f(n,l,e),l.posFrom=l.posTo=n.getCursor(),d(n,t)})})}function d(n,t,r){n.operation(function(){var s=o(n),c=i(n,s.query,t?s.posFrom:s.posTo);(c.find(t)||(c=i(n,s.query,t?e.Pos(n.lastLine()):e.Pos(n.firstLine(),0)),c.find(t)))&&(n.setSelection(c.from(),c.to()),n.scrollIntoView({from:c.from(),to:c.to()},20),s.posFrom=c.from(),s.posTo=c.to(),r&&r(c.from(),c.to()))})}function p(e){e.operation(function(){var n=o(e);n.lastQuery=n.query,n.query&&(n.query=n.queryText=null,e.removeOverlay(n.overlay),n.annotate&&(n.annotate.clear(),n.annotate=null))})}function g(e,n,t){e.operation(function(){for(var o=i(e,n);o.findNext();)if("string"!=typeof n){var r=e.getRange(o.from(),o.to()).match(n);o.replace(t.replace(/\$(\d)/g,function(e,n){return r[n]}))}else o.replace(t)})}function m(e,n){if(!e.getOption("readOnly")){var t=e.getSelection()||o(e).lastQuery,r='<span class="CodeMirror-search-label">'+(n?"Replace all:":"Replace:")+"</span>";c(e,r+y,r,t,function(t){t&&(t=u(t),c(e,x,"Replace with:","",function(o){if(o=a(o),n)g(e,t,o);else{p(e);var r=i(e,t,e.getCursor("from")),s=function(){var n,a=r.from();!(n=r.findNext())&&(r=i(e,t),!(n=r.findNext())||a&&r.from().line==a.line&&r.from().ch==a.ch)||(e.setSelection(r.from(),r.to()),e.scrollIntoView({from:r.from(),to:r.to()}),l(e,C,"Replace?",[function(){c(n)},s,function(){g(e,t,o)}]))},c=function(e){r.replace("string"==typeof t?o:o.replace(/\$(\d)/g,function(n,t){return e[t]})),s()};s()}}))})}}var v='<span class="CodeMirror-search-label">Search:</span> <input type="text" style="width: 10em" class="CodeMirror-search-field"/> <span style="color: #888" class="CodeMirror-search-hint">(Use /re/ syntax for regexp search)</span>',y=' <input type="text" style="width: 10em" class="CodeMirror-search-field"/> <span style="color: #888" class="CodeMirror-search-hint">(Use /re/ syntax for regexp search)</span>',x='<span class="CodeMirror-search-label">With:</span> <input type="text" style="width: 10em" class="CodeMirror-search-field"/>',C='<span class="CodeMirror-search-label">Replace?</span> <button>Yes</button> <button>No</button> <button>All</button> <button>Stop</button>';e.commands.find=function(e){p(e),h(e)},e.commands.findPersistent=function(e){p(e),h(e,!1,!0)},e.commands.findPersistentNext=function(e){h(e,!1,!0,!0)},e.commands.findPersistentPrev=function(e){h(e,!0,!0,!0)},e.commands.findNext=h,e.commands.findPrev=function(e){h(e,!0)},e.commands.clearSearch=p,e.commands.replace=m,e.commands.replaceAll=function(e){m(e,!0)}}),function(e){"function"==typeof e.define&&e.define("addonSearch",["addon/search/search.js"],function(){})}(this);